package top.wbhzx.wangpan.auth;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: JiangZuoWei
 * @Description: 权限拦截器注册器
 * @Date: Create in 09:21 2019-05-28
 */
@Configuration
public class AuthFilterRegistration {

    /**
     * 权限配置
     */
    @Resource
    private AuthConfig authConfig;

    /**
     * 权限服务
     */
    @Resource
    private AuthService authService;

    /**
     * 构造方法初始化
     */
    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        AuthFilter filter = new AuthFilter();
        filter.setAuthConfig(authConfig);
        filter.setAuthService(authService);
        registrationBean.setFilter(filter);
        List<String> urlPatterns;
        // 如果未指定拦截路径，则拦截全部
        if (CollectionUtils.isEmpty(authConfig.getAuthUrls())) {
            urlPatterns = new ArrayList<>();
            urlPatterns.add("/*");
        } else {
            urlPatterns = authConfig.getAuthUrls();
        }
        registrationBean.setUrlPatterns(urlPatterns);
        return registrationBean;
    }

}
